/**
 * Created by zhang_renyang on 14/12/31.
 */
var express = require('express');
var router = express.Router();
var Category = require('../model/category');
var settings = require('../settings');

/**分页查询栏目*/
router.get('/list/:pageNum/:pageSize',function(req,res){
    var pageNum = req.params.pageNum?req.params.pageNum:1;
    var pageSize = req.params.pageSize?req.params.pageSize:settings.pageSize;
    Category.pageQuery({pageNum:pageNum,pageSize:pageSize},function(err,count,categories){
        var totalPage = Math.ceil(count/pageSize);
        res.render('category',{
            user: req.session.user,
            pageNum:pageNum,
            pageSize:pageSize,
            totalPage:totalPage,
            categories:categories
        });
    });
});

/**转入查看栏目的页面*/
router.get('/view/:categoryId',function(req,res){
    Category.findById(req.params.categoryId,function(err,category){
        if(err){
            req.flash('error',err);
            res.redirect('back');
        }else{
            res.render('newCategory',{
                user: req.session.user,
                method : 'view',
                category:category,
                categories:{}
            });
        }
    });
});

/**
 * 跳转到增加页面
 *//*
router.get('/add',function(req,res){
    Category.tree(function(err,categories){
        console.error('router.get(/add'+err);
        res.render('newCategory',{
            user: req.session.user,
            method:'add',
            category:{},
            categories:categories});
    });
});*/

/**增加栏目*/
router.post('/add',function(req,res){
    var category = {
        order_by:req.body.order_by,
        name:req.body.name,
        parent_id:req.body.parent_id
    }
    var newCategory = new Category(category);
    newCategory.save(function(err,category){
        if(err){
            req.flash('error',err);
            res.redirect('back');
        }
        req.flash('success',"添加成功!");
        res.redirect('/category/list/1/'+settings.pageSize);
    });
});

/**转入编辑栏目的页面*/
router.get('/edit/:catgoryId',function(req,res){
    Category.findById(req.params.catgoryId,function(err,category){
        Category.tree(function(err,categories){
            if(err){
                req.flash('error',err);
                res.redirect('back');
            }else{
                res.render('newCategory',{
                    user: req.session.user,
                    category:category,
                    method:'edit',
                    categories:categories
                });
            }
        });
    });
});

/**编辑栏目*/
router.post('/edit',function(req,res){
    var category = {
        order_by:req.body.order_by,
        name:req.body.name,
        parent_id:req.body.parent_id
    };
        var newCategory = new Category(category);
        newCategory.update(req.body._id,function(err,category){
            if(err){
                req.flash('error',err);
                res.redirect('back');
            }
            req.flash('success',"修改成功!");
            res.redirect('/category/list/1/'+settings.pageSize);
        });
});

/**删除栏目*/
router.get('/delete/:categoryId',function(req,res){
    Category.deleteById(req.params.categoryId,function(err){
        if(err){
            req.flash('error',err);
            res.redirect('back');
        }
        req.flash('success',"删除成功!");
        res.redirect('/category/list/1/'+settings.pageSize);
    });
});

/**
 * 跳转到有顶级栏目增加页面
 */
router.get('/adder/:category_id',function(req,res){
    Category.tree(function(err,categories) {
            res.render('newCategory', {
                user: req.session.user,
                method: 'add',
                category:{},

                categories: categories
            })
        }
    )
});
/**
 * 跳转到增加顶级栏目
 */
router.get('/add',function(req,res,next){
    res.render('newCategory',{
        "method":"add",
        category:{},
        categories:{}
    });
});
module.exports = router;